import { RouteRecordRaw } from "vue-router";

/**
 * 去除最外层的布局路由
 * @param routes
 */
export const removeLayoutRoute = (routes: Array<RouteRecordRaw>) => {
  const result: Array<RouteRecordRaw> = [];

  routes.forEach((item) => {
    if (item.path.endsWith("Layout") && item.children) {
      item.children.forEach((child) => result.push(child));
    } else {
      result.push(item);
    }
  });

  return result;
};

/**
 * 将多级嵌套的路由展开为一个平铺的列表
 * @param routes
 */
export const flattenRoutes = (routes: Array<RouteRecordRaw>) => {
  let result: Array<RouteRecordRaw> = [];

  routes.forEach((item) => {
    if (item.children) {
      // 递归展平子项并与当前项连接
      result = result.concat(flattenRoutes(item.children));
    } else {
      // 将当前项添加到展开数组
      result.push(item);
    }
  });

  return result;
};
